/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flight;

import common.PageBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tracy
 */
public class AdvFlightSearchServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String tripType = request.getParameter("triptype");
        String fromWhere = request.getParameter("fromwhere");
        String toWhere = request.getParameter("towhere");
        // String airline = request.getParameter("airline");
        String departDate = request.getParameter("departdate");
        String returnDate = request.getParameter("returndate");
        String departTime = request.getParameter("departtime");
        String departArrivalTime = request.getParameter("departarrivaltime");
        String returnTime = request.getParameter("returntime");
        String returnArrivalTime = request.getParameter("returnarrivaltime");
        String fuzzyLeaveDate = request.getParameter("fuzzyleavedate");
        String fuzzyReturnDate = request.getParameter("fuzzyreturndate");
        String noOfPassengers = request.getParameter("noofpassengers");
        String seatingClass = request.getParameter("seatingclass");
        String nonStop = request.getParameter("nonstop");
        String showroundabout = request.getParameter("showroundabout");

        String searchCriteria = "";
        String pageNum = null;
        
        FlightBean fb = new FlightBean();
        StringTokenizer st = null;
        
        fb.setTripType(Integer.parseInt(tripType));
        
        if (fb.getTripType() == 1)
            searchCriteria += "Round-trip; ";
        else
            searchCriteria += "Single-way; ";
        
        fb.setDepartAirport(fromWhere);
        fb.setDepartCity(fromWhere);
        fb.setArriveAirport(toWhere);
        fb.setArriveCity(toWhere);
        
        searchCriteria += "From: " + fromWhere + "; To: " + toWhere + "; ";
        
//        fb.setAirline(airline);
//
//        if (airline.equals("0")) {
//           fb.setSpecifiedAirline(false);
//        }
//        else if (departTime.equals("1")) {
//            fb.setSpecifiedAirline(true);
//            fb.setAirline("");
//            searchCriteria += "Airline: " + airline + "; ";
//        }
//        else if (departTime.equals("2")) {
//            fb.setSpecifiedAirline(true);
//            fb.setAirline("");
//            searchCriteria += "Airline: " + airline + "; ";
//        }
//        else if (departTime.equals("3")) {
//            fb.setSpecifiedAirline(true);
//            fb.setAirline("");
//            searchCriteria += "Airline: " + airline + "; ";
//        }
//        else if (departTime.equals("4")) {
//            fb.setSpecifiedAirline(true);
//            fb.setAirline("");
//            searchCriteria += "Airline: " + airline + "; ";
//        }
        
        st = new StringTokenizer(departDate,"-");
        fb.setDepartMonth(Integer.parseInt(st.nextToken()));
        fb.setDepartDay(Integer.parseInt(st.nextToken()));
        fb.setDepartYear(Integer.parseInt(st.nextToken()));
        
        if (departTime.equals("0")) {
            fb.setDepartHourLower(0);
            fb.setDepartHourUpper(24);
        }
        else if (departTime.equals("1")) {
            fb.setDepartHourLower(0);
            fb.setDepartHourUpper(6);
        }
        else if (departTime.equals("2")) {
            fb.setDepartHourLower(6);
            fb.setDepartHourUpper(12);
        }
        else if (departTime.equals("3")) {
            fb.setDepartHourLower(12);
            fb.setDepartHourUpper(18);
        }
        else if (departTime.equals("4")) {
            fb.setDepartHourLower(18);
            fb.setDepartHourUpper(24);
        }
        
        searchCriteria += "<br> Depart date: " + fb.getDepartYear() + "-" + fb.getDepartMonth() + "-" + fb.getDepartDay() + "; ";
        searchCriteria += "Depart time: " + fb.getDepartHourLower() + ":00:00" +  " to " + fb.getDepartHourUpper() + ":00:00" + "; ";
        
        if (departArrivalTime.equals("0")) {
            fb.setDepartArrivalHourLower(0);
            fb.setDepartArrivalHourUpper(24);
        }
        else if (departArrivalTime.equals("1")) {
            fb.setDepartArrivalHourLower(0);
            fb.setDepartArrivalHourUpper(6);
        }
        else if (departArrivalTime.equals("2")) {
            fb.setDepartArrivalHourLower(6);
            fb.setDepartArrivalHourUpper(12);
        }
        else if (departArrivalTime.equals("3")) {
            fb.setDepartArrivalHourLower(12);
            fb.setDepartArrivalHourUpper(18);
        }
        else if (departArrivalTime.equals("4")) {
            fb.setDepartArrivalHourLower(18);
            fb.setDepartArrivalHourUpper(24);
        }

        searchCriteria += "Depart arrival time: " + fb.getDepartArrivalHourLower() + ":00:00" +  " to " + fb.getDepartArrivalHourUpper() + ":00:00" + "; ";
        
        if (fuzzyLeaveDate != null)
        {
            fb.setFuzzyLeaveDate(true);
            searchCriteria += "Flexible depart date; ";
        }
        
        // is round-trip
        if (fb.getTripType() == 1) {
            st = new StringTokenizer(returnDate, "-");
            fb.setReturnMonth(Integer.parseInt(st.nextToken()));
            fb.setReturnDay(Integer.parseInt(st.nextToken()));
            fb.setReturnYear(Integer.parseInt(st.nextToken()));
        
            if (returnTime.equals("0")) {
                fb.setReturnHourLower(0);
                fb.setReturnHourUpper(24);
            }
            else if (returnTime.equals("1")) {
                fb.setReturnHourLower(0);
                fb.setReturnHourUpper(6);
            }
            else if (returnTime.equals("2")) {
                fb.setReturnHourLower(6);
                fb.setReturnHourUpper(12);
            }
            else if (returnTime.equals("3")) {
                fb.setReturnHourLower(12);
                fb.setReturnHourUpper(18);
            }
            else if (returnTime.equals("4")) {
                fb.setReturnHourLower(18);
                fb.setReturnHourUpper(24);
            }

            searchCriteria += "<br> Return date: " + fb.getReturnYear() + "-" + fb.getReturnMonth() + "-" + fb.getReturnDay() + "; ";
            searchCriteria += "Return time: " + fb.getReturnHourLower() + ":00:00" +  " to " + fb.getReturnHourUpper() + ":00:00" + "; ";

            if (returnArrivalTime.equals("0")) {
                fb.setReturnArrivalHourLower(0);
                fb.setReturnArrivalHourUpper(24);
            }
            else if (returnArrivalTime.equals("1")) {
                fb.setReturnArrivalHourLower(0);
                fb.setReturnArrivalHourUpper(6);
            }
            else if (returnArrivalTime.equals("2")) {
                fb.setReturnArrivalHourLower(6);
                fb.setReturnArrivalHourUpper(12);
            }
            else if (returnArrivalTime.equals("3")) {
                fb.setReturnArrivalHourLower(12);
                fb.setReturnArrivalHourUpper(18);
            }
            else if (returnArrivalTime.equals("4")) {
                fb.setReturnArrivalHourLower(18);
                fb.setReturnArrivalHourUpper(24);
            }
            
            searchCriteria += "Return arrival time: " + fb.getReturnArrivalHourLower() + ":00:00" +  " to " + fb.getReturnArrivalHourUpper() + ":00:00" + "; ";
            
            if (fuzzyReturnDate != null)
            {
                fb.setFuzzyReturnDate(true);
                searchCriteria += "Flexible return date; ";
            }
        }
        
//        if (fuzzyReturnDate != null)
//            fb.setFuzzyReturnDate(true);

        if (Integer.parseInt(seatingClass) == 1)
            fb.setSeatingClass("Coach");
        else if (Integer.parseInt(seatingClass) == 2)
            fb.setSeatingClass("Bussiness");
        else
            fb.setSeatingClass("First");

        searchCriteria += "<br> Seating: " + fb.getSeatingClass() + "; ";
        
        fb.setNoOfTickets(Integer.parseInt(noOfPassengers));
        
        searchCriteria += "Number of Passengers: " + fb.getNoOfTickets() + "; ";

        if (nonStop != null)
        {
            fb.setNonStop(true);
            searchCriteria += "Prefer non-stop; ";
        }
        else
            fb.setNonStop(false);

        if (showroundabout != null)
        {
            fb.setShowRoundabout(true);
            searchCriteria += "Show roundabout trips; ";
        }
        else
            fb.setShowRoundabout(false);

        fb.setSearchCriteria(searchCriteria);

        try {
            FlightPageBean fpb = new FlightPageBean();
            pageNum = (String)request.getParameter("jumpPage");
            if (pageNum == null)
                pageNum = "1";
            fpb.setFb(fb);          // set searching information bean
            fpb.setTripType(fb.getTripType());  // set trip type
            fpb.setCurrentType(1);
            PageBean pageCtl = fpb.listData(pageNum);
            request.setAttribute("pageCtl",pageCtl); // store PageBean object in request
            request.setAttribute("fb",fb);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        String dispatchPage = "";

        if (fb.getTripType() == 1)
            dispatchPage = "/rightpanels/results_depart_flight_right.jsp";
        else if (fb.getTripType() == 2)
            dispatchPage = "/rightpanels/results_flight_right.jsp";
            
        RequestDispatcher dis = request.getRequestDispatcher(dispatchPage);
        dis.forward(request,response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}